package com.xnyzc.lhy.order.service.impl.user.tab;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.order.entity.user.OaCUser;
import com.xnyzc.lhy.order.entity.user.tab.OaTabPermission;
import com.xnyzc.lhy.order.entity.user.tab.OaTabUser;
import com.xnyzc.lhy.order.mapper.user.OaCUserMapper;
import com.xnyzc.lhy.order.mapper.user.tab.OaTabPermissionMapper;
import com.xnyzc.lhy.order.mapper.user.tab.OaTabUserMapper;
import com.xnyzc.lhy.order.service.user.tab.IOaTabPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * Tab权限配置表 服务实现类
 * </p>
 *
 * @author bin
 * @since 2019-07-23
 */
@Service
public class OaTabPermissionServiceImpl extends ServiceImpl<OaTabPermissionMapper, OaTabPermission> implements IOaTabPermissionService {

    @Autowired
    private OaTabPermissionMapper oaTabPermissionMapper;
    @Autowired
    private OaTabUserMapper oaTabUserMapper;
    @Autowired
    private OaCUserMapper oaCUserMapper;

    /**
     * 获取tab信息(温馨提示)
     * @param userId 用户id
     * @param tabId 标签id
     * @return
     */
    @Override
    public Map<String, Object> getTabContent(long userId, int tabId){
        Map<String, Object> result = new HashMap<>(1);

        // 查询当前tab关联信息
        OaTabUser oaTabUser = oaTabUserMapper.selectOne(
                Wrappers.<OaTabUser>lambdaQuery()
                        .eq(OaTabUser::getUserId, userId)
                        .eq(OaTabUser::getTabId, tabId)
        );
        if (oaTabUser == null){
            return result;
        }

        // 查询当前tab配置信息
        OaTabPermission oaTabPermission = this.getTabContent(oaTabUser.getUserType(), tabId);
        if (oaTabPermission == null){
            return result;
        }
        result.put("code", 0);
        result.put("mes", oaTabPermission.getTabHint());
        return result;
    }

    /**
     * 根据用户类型与tabId获取tab信息
     * @param userType 用户类型
     * @param tabId 标签id
     * @return
     */
    private OaTabPermission getTabContent(int userType, int tabId) {
        return oaTabPermissionMapper.selectOne(
                Wrappers.<OaTabPermission>lambdaQuery()
                        .eq(OaTabPermission::getTabId, tabId)
                        .eq(OaTabPermission::getUserType, userType)
        );
    }
}
